package com.example.superoa.model.service;

import java.sql.Time;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.dao.KqAttendanceDao;
import com.example.superoa.model.dao.StaffDao;
import com.example.superoa.model.mappers.AttendMapper;
import com.example.superoa.model.mappers.KqAttendanceMapper;
import com.example.superoa.model.mappers.OvertimeMapper;
import com.example.superoa.model.pojos.KqAttendance;
import com.example.superoa.model.pojos.Staff;

@Service
@Transactional(rollbackFor=Exception.class)
public class AttendService {
	@Autowired
	private KqAttendanceDao dao;
	
	@Autowired
	private DakaService d_biz;
	
	@Autowired
	private OvertimeMapper o_map;
	
	@Autowired
	private HrDepartService h_ser;
	
	@Autowired
	private StaffDao s_dao;
	
	
	Shijian sj=new Shijian();
	
	@Autowired
	private AttendMapper map;
	//查询所有打卡记录
	public JSONArray selectDAll(){
		return map.selectDAll();
	}
	//查询员工
	public JSONObject selectstaff(Integer staff_id) {
		return map.selectstaff(staff_id);
	}
	//搜索
	public JSONArray selectSousuo(Date kai,Date jie,int id) {
		return map.selectSousuo(kai,jie,id);
	}
	//查询所有补卡记录
	public JSONArray selectRAll() {
		return map.selectRAll();
	}
	//根据输入时间查询补卡记录
	public JSONArray selectRdate(Date kai,Date jie) {
		return map.selectRdate(kai,jie);
	}
	
	//根据id查询补卡记录
	public JSONObject selectID(int id) {
		return map.selectid(id);
	}
	//判断补卡
	public String panduan(Date date,Integer staff_id) {
		JSONObject attend=map.selectattendidmont(staff_id, date);
		JSONArray jiaban=o_map.selectOvertimeStaff1(staff_id);
		if(attend!=null) {
			if(attend.get("a_dao")!=null&&attend.get("a_tui")!=null) {
				return "你选择的补卡的日期中有你的考勤记录";
			}
		}
		for (Object object : jiaban) {
			JSONObject obj=(JSONObject)object;
			if(sj.belongCalendar(date, obj.getDate("O_KAI"), obj.getDate("O_JIE"), 2)) {
				return "加班期间的考勤不能补卡";
			}
		}
		return "";
		
	}
	//添加考勤补卡记录
	public void add(KqAttendance att,int staff_id) {
		att.setABan(1);
		att.setAChu(2);
		att.setAShen(0);
		att.setAShendate(new Date(sj.getNetworkTime()));
		att.setStaff(s_dao.findById(staff_id).get());
		att.setAXshen(selectDeptStaff(staff_id)+"");
		dao.save(att);
	}
	//查询考勤记录最后一条id
	public Integer selectattendid() {
		return map.selectattendid();
	}
	//查询部门负责人
	public String selectDeptStaff(Integer staff_id) {
		JSONObject obj=map.selectstaff(staff_id);
		return h_ser.selectStaffByDepartname(obj.getInteger("Depart_id")).getJSONObject(0).getString("staff_id");
	}
	public Integer selectattendidmont(Integer staff_id,Date date) {
		return map.selectattendidmont(staff_id, date).getInteger("a_id");
	}
	//补卡审批
	public void shen(int id,Integer shen,String str) {
		KqAttendance pojos=dao.findById(id).get();
		if(shen==2) {
			pojos.setABuo(str);
		}
		pojos.setaSdate(new Date(sj.getNetworkTime()));
		pojos.setAShen(shen);
		dao.save(pojos);
		if(shen==1) {
			System.out.println(map.selectattendidmont(pojos.getStaff().getStaffId(),pojos.getADate()));
			KqAttendance pojo=new KqAttendance();
			Integer attid=map.selectattendidmont(pojos.getStaff().getStaffId(),pojos.getADate()).getInteger("a_id");
			if(attid!=null) {
				pojo=dao.findById(attid).get();
				if(pojo.getADao()==null) {
					pojo.setADao(pojos.getADao());
				}
				if(pojo.getATui()==null) {
					pojo.setATui(pojos.getATui());
				}
			}else {
				pojo=pojos;
				pojo.setAId(selectattendid()+1);
			}
			pojo.setABan(2);
			pojo.setAChu(1);
			pojo.setAShen(1);
			pojo.setAWhy(pojos.getAWhy());
			d_biz.Dao(pojo.getStaff().getStaffId(), pojo.getADao(), pojo.getADate(), pojo);
			d_biz.Tui(pojo, pojos.getATui());
		}
	}
}
